package com.qianji.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.github.pagehelper.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author 钟丽诗
 * @Date 2021.3.23
 * OSS上传图片
 */

@Slf4j
@RestController
@RequestMapping("/oss/")
public class OSSUploadController {

    /**
     * OSS地域节点
     */
    @Value("${oss.endPoint}")
    private String endPoint;

    /**
     * 秘钥
     */
    @Value("${oss.accessKeyId}")
    private String accessKeyId;

    /**
     * 密匙
     */
    @Value("${oss.accessKeySecret}")
    private String accessKeySecret;

    /**
     * bucket名称
     */
    @Value("${oss.bucketName}")
    private String bucketName;

    /**
     * 文件上传（上传图片）
     * @param file
     * @return
     */
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file){

        // 设置URL过期时间为10年 3600l* 1000*24*365*10
        Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);

        Map<String, Object> resultMap = new HashMap<>();

        OSS ossClient = null;

        try{

            ossClient = new OSSClientBuilder().build(endPoint,accessKeyId,accessKeySecret);;

            String fileName = UUID.randomUUID().toString().replaceAll("-", "") + ".png";

            InputStream inputStream = file.getInputStream();

            PutObjectRequest request  = new PutObjectRequest(bucketName,fileName,inputStream);

            log.info("request:{}",request.getFile());

            ossClient.putObject(request);

            String url = "https://"+bucketName+"."+endPoint+"/"+fileName;

            resultMap.put("code",200);
            resultMap.put("url",url);
            resultMap.put("message","图片上传成功");
        }catch (Exception e){
            resultMap.put("code",500);
            resultMap.put("message","图片上传失败");
            e.printStackTrace();
        }finally {

            ossClient.shutdown();

        }

        return resultMap;

    }

    /**
     * 删除OSS中的文件（图片）
     * @param filePath 文件名称
     * @return
     */
    @ApiOperation("删除OSS文件")
    @RequestMapping(value = "deleteFile",method = RequestMethod.POST)
    public Map<String, Object> deleteFile(String filePath){

        OSS ossClient = new OSSClientBuilder().build(endPoint,accessKeyId,accessKeySecret);

        Map<String , Object> resultMap = new HashMap<>();

        boolean result = ossClient.doesObjectExist(bucketName,filePath);

        if(!result){
            resultMap.put("success",false);
            resultMap.put("message","您输入的文件名称不存在！！！");
            return  resultMap;
        }

        resultMap.put("success",true);
        resultMap.put("message","文件删除成功！！！");

        ossClient.deleteObject(bucketName,filePath);
        ossClient.shutdown();

        return resultMap;

    }

}
